function [centers, success_counter, occupied_map, available_map] = generate_random_speckles(...
    centers, occupied_map, available_map, X, Y, EMSD, failure_limit)
success_counter = 0;  % Speckle coordinate count
failure_counter = 0;  % Failure count for generation

while true
    % Find available pixels
    [available_pixels_coords, available_length] = get_available_pixels(available_map);
    
    % Randomly choose an available pixel
    new_center_idx = ceil(available_length * rand);
    if new_center_idx == 0
        new_center_idx = new_center_idx + 1;
    end
    
    % Generate sub-pixel coordinates
    new_center = [available_pixels_coords(new_center_idx, 2) - rand, ...
        available_pixels_coords(new_center_idx, 1) - rand];
    
    % Check for overlap with occupied region
    if ~is_overlapping(new_center, occupied_map, X, Y, EMSD)
        success_counter = success_counter + 1;
        centers(success_counter, :) = new_center;  % Add new center
        [occupied_map, available_map] = update_maps(...
            new_center, occupied_map, available_map, X, Y, EMSD);
        
        failure_counter = 0;
    else
        failure_counter = failure_counter + 1;
    end
    
    % If failure count exceeds threshold, stop the loop
    if failure_counter >= failure_limit
        break;
    end
end
end